﻿#region Copyright (C) Sector7 Software Studio. All rights reserved
/*******************************************************************************************
 * Creation
 *      Author:         Wolf Lai
 *      Date:           2013/8/6 星期二 23:26:30
 *      Version:        
 * Modification 
 *      Author: 
 *      Date: 
 *      Description: 
 *      Version:
 *******************************************************************************************/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Medical.Biz;
using Sector7.Framework.Utility;
using Sector7.Medical.EntityType;
using Sector7.Medical.Consts;
using Sector7.Medical.Enums;
using System.Collections;

namespace Sector7.Medical.Controls
{
    /// <summary>
    /// 药品信息下拉框控件
    /// </summary>
    public class DrugComboBox : ComboBox
    {
        #region Accessor
        /// <summary>
        /// 获取或设置模板类型
        /// </summary>
        public DrugTemplateType TemplateType
        {
            get { return m_tempalte; }
            set { m_tempalte = value; }
        }
        #endregion

        #region Structure
        public DrugComboBox()
        {
            IsEditable = true;
            BorderBrush = null;
            DisplayMemberPath = C_ColumnName.DRUG_NAME;
            SelectedValuePath = C_ColumnName.DRUG_ID;
            
            KeyDown += new KeyEventHandler(DrugComboBox_KeyDown);
        }

        void DrugComboBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                e.Handled = true;
                if (!this.IsDropDownOpen)
                {
                    string keywords = this.Text;

                    if (string.IsNullOrWhiteSpace(keywords))
                    {
                        Alert.ShowDialog(AlertType.Error, "请输入药品拼音码或编号！");
                    }
                    else
                    {
                        Array dataArray = null;

                        switch (m_tempalte)
                        {
                            case DrugTemplateType.Unknown:
                                dataArray = Agent_INFS.GetDrugListByKeywords(keywords).ToArray();
                                break;
                            case DrugTemplateType.PurchasesBill:
                                dataArray = Agent_STOS.GetDrugListForPurchasesBill(keywords).ToArray();
                                break;
                            case DrugTemplateType.AdjustRecord:
                                dataArray = Agent_STOS.GetDrugPriceListByKeywords(keywords).ToArray();
                                break;
                            default:
                                break;
                        }

                        if (dataArray == null || dataArray.Length == 0)
                        {
                            Alert.ShowDialog(AlertType.Warning, "没有找到指定项！");
                        }
                        else
                        {
                            ClientUtil.LoadItemsSource(this, dataArray);
                            this.IsDropDownOpen = true;
                        }
                    }
                }
            }
        }
        #endregion

        #region Inner members
        private DrugTemplateType m_tempalte = DrugTemplateType.Unknown;
        #endregion
    }
}
